package com.ruoyi.project.tool.swagger.AppApi;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.SysAlarm;
import com.ruoyi.project.system.domain.SysDictData;
import com.ruoyi.project.system.domain.vo.SysAlarmVo;
import com.ruoyi.project.system.service.ISysAlarmService;
import com.ruoyi.project.system.service.ISysDictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;



@RestController
@RequestMapping("appapi/")
@Api(description = "查询报警记录")
public class SysUserPostController extends BaseController {

    @Autowired
    private ISysAlarmService sysAlarmService;

    @Autowired
    private ISysDictDataService dictDataService;
    @PostMapping("selectAlaryTotal")
    @ApiOperation("查询异常最新消息")
    public AjaxResult selectAlaryTotal(){
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        List<SysDictData> list=dictDataService.selectDictDataByType("alarm_type");
        if(!list.isEmpty()){
            for(SysDictData sysDictData:list){
                List<SysAlarm> sysAlarms = sysAlarmService.selectSysAlarmById(userId.toString(),sysDictData.getDictValue(),"0");
                if(!sysAlarms.isEmpty()){
                    sysDictData.setTotal(sysAlarms.size());
                    sysDictData.setAddress(sysAlarms.get(0).getAddress());
                    sysDictData.setCreateTime(sysAlarms.get(0).getCreateTime());
                }else{
                    sysAlarms = sysAlarmService.selectSysAlarmById(userId.toString(),sysDictData.getDictValue(),"1");
                    if(!sysAlarms.isEmpty()){
                        sysDictData.setTotal(null);
                        sysDictData.setAddress(sysAlarms.get(0).getAddress());
                        sysDictData.setCreateTime(sysAlarms.get(0).getCreateTime());
                    }else{
                        sysDictData.setCreateTime(null);
                    }

                }
            }
        }
        return AjaxResult.success(list);
    }


    @PostMapping("selectAlary")
    @ApiOperation("异常类型点击接口")
    public AjaxResult selectAlary(@RequestBody(required = false) SysAlarmVo sysAlarm){
        if(null==sysAlarm){
            return AjaxResult.error("参数错误");
        }
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        List<SysAlarm> sysAlarms = sysAlarmService.selectSysAlarmById(userId.toString(),sysAlarm.getAlarmType(),null);
        return AjaxResult.success(sysAlarms);
    }


}